package com.djx.基础;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;

/**
 * 获取子节点数据
 */
public class ZKGetChildren {

    ZooKeeper zooKeeper = new MyZookepper01().zooKeeper;

    @Test
    public void syncGetChirldren() throws KeeperException, InterruptedException {
        List<String> children = zooKeeper.getChildren("/djx", false, null);
        for (String child : children) {
            System.out.println("子节点为 " + child);
            System.out.println(new String(zooKeeper.getData("/djx/"+child, false, null)));
        }
        zooKeeper.close();
        System.out.println("zookeeper关闭成功");
    }

    @Test
    public void asyncGetChirldren() throws KeeperException, InterruptedException {
        zooKeeper.getChildren("/djx", false, new AsyncCallback.Children2Callback() {
            public void processResult(int i, String s, Object o, List<String> list, Stat stat) {
                System.out.println(i);
                System.out.println(o);

                System.out.println(Arrays.asList(list));

            }
        }, "查询成功");
        Thread.sleep(50000);
        zooKeeper.close();
        System.out.println("zookeeper关闭成功");
    }


}
